package com.rybbaby.tss.platform.sysAdminUser.dao.imp;

import com.rybbaby.tss.core.web.dao.imp.GenericDao;
import com.rybbaby.tss.platform.sysAdminUser.dao.ISysAdminUserDao;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @description 管理员基本信息数据库操作实现
 * 
 * @author 谢进伟
 * 
 * @createDate 2016-11-22 下午1:36:42
 */
@Repository
public class SysAdminUserDaoImp extends GenericDao<SysAdminUser> implements ISysAdminUserDao {

    @Override
    public Integer updateIsForbidden(String targetDateId,Boolean isForbidden) {
        return this.executeSql("update sys_admin_user set isForbidden=? where id=?",new Object[]{isForbidden,targetDateId});
    }

    /**
     * 根据状态来修改课时包信息
     *
     * @param targetDateId
     * @param isBlockUp
     * @return
     */
    @Override
    public Integer updateStaffByIsBlockUp(String targetDateId, Boolean isBlockUp) {
        return this.executeSql("update tss_staff set updateTime=Now(),isBlockUp=? where id=?",new Object[]{isBlockUp,targetDateId});
    }

    @Override
    public List<SysAdminUser> getAllList() {
        return this.loadAll(SysAdminUser.class);
    }

    @Override
    public List<SysAdminUser> findListByIds(List<String> ids) {
        return this.loadByMultipleId(SysAdminUser.class, ids);
    }


    /**
     * 根据名称查询ID
     * 杨然
     * */
    @Override
    public String findStaffIdByName(String staffName) {
        String sql = "select a.id from sys_admin_user a where a.realName in('"+staffName+"')";

        try {
            List<SysAdminUser> listBySql = super.findListBySql(sql, SysAdminUser.class, null);
            if (!listBySql.isEmpty()){
                return listBySql.get(0).getId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SysAdminUser> getPostListByDuty(Integer duty,String schoolId) throws Exception {
        StringBuffer buffer=new StringBuffer();
        buffer.append("SELECT id,realName FROM sys_admin_user WHERE post IS NOT NULL  and schoolId='"+schoolId+"'");
        if(duty==4){
           buffer.append(" AND (post ="+duty+"  OR partTime LIKE '%"+duty+"%'  OR post='3' OR partTime LIKE '%3%')");
        }else if(duty==6){
            buffer.append(" AND (post ="+duty+"  OR partTime LIKE '%"+duty+"%'  OR post='5' OR partTime LIKE '%5%')");
        }
        return this.findListBySql(buffer.toString(),SysAdminUser.class,null);
    }

    @Override
    public List<SysAdminUser> findByUserNamePassWord(String userName, String password) throws Exception {
        String sql="SELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\tsys_admin_user\n" +
                "WHERE\n" +
                "\tsys_admin_user.userName = '"+userName+"'\n" +
                "AND sys_admin_user.userPassword = '"+password+"'";
        System.out.println(sql);
        return this.findListBySql(sql,SysAdminUser.class,null);
    }

    @Override
    public List<SysAdminUser> findByuserName(String userName) throws Exception {
        String sql="SELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\tsys_admin_user\n" +
                "WHERE\n" +
                "\tsys_admin_user.userName = '"+userName+"'\n";
        System.out.println(sql);
        return this.findListBySql(sql,SysAdminUser.class,null);
    }

    @Override
    public Map<String, Object> findSchoolCode(String adminUserId) {
        String sql = "SELECT\n" +
                "\tsys_school.schoolCode\n" +
                "FROM\n" +
                "\tsys_admin_user\n" +
                "LEFT JOIN sys_school ON sys_school.id = sys_admin_user.schoolId\n" +
                "WHERE\n" +
                "\tsys_admin_user.id = ?";
        return this.findOneForJdbc(sql,new Object[]{adminUserId});
    }


}
